﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using Public;


namespace SalomonWebsite.UC
{
    public partial class PageNote : System.Web.UI.UserControl
    {
        #region //封装字段

        private int _parid;

        public int ParPKID
        {
            get { return _parid; }
            set { _parid = value; }
        }
        
        private int _pkid;

        public int PKID
        {
            get { return _pkid; }
            set { _pkid = value; }
        }
        #endregion

        DataTable dtRtn = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            GetCatList(PKID);
            if (dtRtn.Rows.Count > 0)
            {
                DataView dv = dtRtn.DefaultView;
                dv.Sort = "CatRank ASC";
                this.rpCategory.DataSource = dv;
                this.rpCategory.DataBind();
                dtRtn.Dispose();
            }
        }

        protected void GetCatList(int ParID)
        {
            DataTable dt = Bll.Category.MULTIPLE(Global.Connection);
            if (dt.Rows.Count > 0)
            {

                // Add three column objects to the table.
                DataColumn dc1 = new DataColumn();
                dc1.DataType = System.Type.GetType("System.Int32");
                dc1.ColumnName = "PKID";
                //dc1.AutoIncrement = true;
                dtRtn.Columns.Add(dc1);

                DataColumn dc2 = new DataColumn();
                dc2.DataType = System.Type.GetType("System.String");
                dc2.ColumnName = "CatName";
                //dc2.AutoIncrement = true;
                dtRtn.Columns.Add(dc2);



                DataColumn dc3 = new DataColumn();
                dc3.DataType = System.Type.GetType("System.Int32");
                dc3.ColumnName = "ParID";
                //dc3.AutoIncrement = true;
                dtRtn.Columns.Add(dc3);

                DataColumn dc4 = new DataColumn();
                dc4.DataType = System.Type.GetType("System.Int32");
                dc4.ColumnName = "CatRank";
                //dc4.AutoIncrement = true;
                dtRtn.Columns.Add(dc4);

                DataColumn dc5 = new DataColumn();
                dc5.DataType = System.Type.GetType("System.Int32");
                dc5.ColumnName = "SortBy";
                //dc5.AutoIncrement = true;
                dtRtn.Columns.Add(dc5);

                DataColumn dc6 = new DataColumn();
                dc6.DataType = System.Type.GetType("System.Int32");
                dc6.ColumnName = "Num";
                //dc5.AutoIncrement = true;
                dtRtn.Columns.Add(dc6);


                DataColumn dc7 = new DataColumn();
                dc7.DataType = System.Type.GetType("System.String");
                dc7.ColumnName = "CatCode";
                //dc2.AutoIncrement = true;
                dtRtn.Columns.Add(dc7);

                GetCatData(PKID, dt, dtRtn);

                //if (dtRtn.Rows.Count > 0)
                //{

                //}

            }
        }

        protected void GetCatData(int parID, DataTable dtCat, DataTable dtRtn)
        {
            DataRow[] drs = dtCat.Select(" [PKID] = " + parID);

            for (int i = 0; i < drs.Length; i++)
            {
                DataRow dr = dtRtn.NewRow();
                dr["PKID"] = Convert.ToInt32(drs[i]["PKID"]);
                dr["CatName"] = drs[i]["CatName"].ToString();
                dr["ParID"] = Convert.ToInt32(drs[i]["ParID"]);
                dr["CatRank"] = Convert.ToInt32(drs[i]["CatRank"]);
                dr["SortBy"] = Convert.ToInt32(drs[i]["SortBy"]);
                dr["Num"] = Convert.ToInt32(drs[i]["Num"]);
                dr["CatCode"] = drs[i]["CatCode"].ToString();
                dtRtn.Rows.Add(dr);
                GetCatData(Convert.ToInt32(drs[i]["ParID"]), dtCat, dtRtn);
            }
        }


        protected void RP_Cat_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView rowv = (DataRowView)e.Item.DataItem;

                Label lblLink = ((Label)e.Item.FindControl("Link"));
                if (Convert.ToInt32(rowv["CatRank"]) == 1)
                {
                    lblLink.Text = rowv["CatName"].ToString();
                }
                else
                {
                    if (Convert.ToInt32(rowv["Num"]) == 0)
                    {
                        lblLink.Text = "&gt; <a href=\"/Ser-" + ParPKID + "-" + rowv["PKID"] + "-" + rowv["CatCode"] + ".aspx\">" + rowv["CatName"].ToString() + "</a>";
                    }
                    else
                    {
                        lblLink.Text = "&gt; <a href=\"/Cat-" + ParPKID + "-" + rowv["PKID"] + "-" + rowv["CatCode"] + ".aspx\">" + rowv["CatName"].ToString() + "</a>";
                    }
                }
            }
        }
    }
}